//
//  CustomTabelViewUtil.m
//  YiJuZhangShou
//
//  Created by URANUS on 16/3/17.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "LoadTableUtilView.h"
#import "UITableViewCell+Custom.h"
#import "Masonry.h"
@interface LoadTableUtilView()<UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation LoadTableUtilView

#pragma mark Delegate UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    id<CustomCellModel> model = _models[indexPath.row];
    UITableViewCell *cell = [UITableViewCell tableView:tableView cellWithModel:model];
    UIView *view = [[UIView alloc] initWithFrame:cell.frame];
    view.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
    cell.selectedBackgroundView = view;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_models.count != 0) {
       return _models.count;
    }
    return 0;
}

#pragma mark Public
- (UITableView *)loadTableViewWithHeaderShowViewWithTitle:(NSString *)title
                                           withTitleColor:(UIColor *)titleColor
                                         andIndictorTitle:(NSString *)indictorTitle{
    UIView *topView = [UIView new];
    topView.backgroundColor = [UIColor whiteColor];
    topView.tag = TableTopViewTag;
    [self addSubview:topView];
    self.indicatorView = [ViewUtil loadSalersTitleWithBackView:topView withTitle:title andTitleColor:titleColor];
    self.indicatorView.rightText = indictorTitle;
    self.indicatorView.tag = MoreIndicatorTag;
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(ScreenHeight*0.07).priorityLow();
        make.height.lessThanOrEqualTo(self);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom);
        make.left.right.equalTo(topView);
        make.bottom.equalTo(self);
    }];
    
    return _tableView;
}

- (UITableView *)loadStandardTableView{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    return self.tableView;
}

#pragma mark Getter
-(UITableView *)tableView{
    
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 60;
        _tableView.tag = TableTag;
        _tableView.showsVerticalScrollIndicator = NO;
        [self addSubview:_tableView];
    }
    return _tableView;
}

-(void)setModels:(NSMutableArray *)models{
    _models = models;
    [self.tableView reloadData];
    if (_isTableViewSizeToFit == YES) {
        [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.tableView.contentSize.height);
        }];
        [self layoutIfNeeded];
        [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.tableView.contentSize.height);
        }];
    }
    
}

@end
